<?php
/*
+--------------------------------------------------------------------------
|   WeCenter [#RELEASE_VERSION#]
|   ========================================
|   by WeCenter Software
|   © 2011 - 2014 WeCenter. All Rights Reserved
|   http://www.wecenter.com
|   ========================================
|   Support: WeCenter@qq.com
|
+---------------------------------------------------------------------------
*/


if (!defined('IN_ANWSION'))
{
	die;
}

class main extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action['rule_type'] = "white";

		if ($this->user_info['permission']['search_avail'] AND $this->user_info['permission']['visit_site'])
		{
			$rule_action['rule_type'] = "black"; //'black'黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		}

		$rule_action['actions'] = array();

		return $rule_action;
	}

	public function setup()
	{
		HTTP::no_cache_header();

		$this->crumb(AWS_APP::lang()->_t('搜索'), '/search/');
	}

	//商品搜索
    public function g_action(){

        if ($_POST['q'])
        {
            $url = '/search/g/q-' . base64_encode($_POST['q']);

            if ($_GET['is_recommend'])
            {
                $url .= '__is_recommend-1';
            }

            HTTP::redirect($url);
        }

        $keyword = htmlspecialchars(base64_decode($_GET['q']));
        $this->crumb($keyword, '/search/g/q-' . urlencode($keyword));

        if (!$keyword)
        {
            HTTP::redirect('/');
        }

        TPL::assign('keyword', $keyword);
        TPL::assign('split_keyword', implode(' ', $this->model('system')->analysis_keyword($keyword)));

//      搜索
        $where[]= "goods_name LIKE '%" .$keyword. "%'";


        $where[] = " is_delete= 0 ";
        $where[] = " is_on_sale = 1 ";

        if($_GET['cat']){
            $where[] = 'cat_id ='.intval($_GET['cat']);
        }
        $cols = "goods_id,cat_id,goods_sn,goods_name,keywords,goods_brief,market_price,shop_price,promote_price,give_integral,sort_order,is_new,is_best,is_hot,is_promote,jd_url,tb_url,cover";
        $cols  = explode(',',$cols);

        $data = $this->model('goods')->get_goods_list_page($cols,$where,' goods_id DESC');
        foreach ($_GET as $key => $val) {
            if (!in_array($key, array('app', 'c', 'act', 'page'))) {
                $url_param[] = $key . '-' . $val;
            }
        }

        $total_rows = $data['total_rows'];
        $goods_list = $data['list'];
        TPL::assign('pagination', AWS_APP::pagination()->initialize(array(
            'base_url' => get_js_url('product/all/') . implode('__', $url_param),
            'total_rows' => $total_rows,
            'per_page' => $this->per_page
        ))->create_links());

        TPL::assign('goods_list', $goods_list);
        TPL::assign('total_rows', $total_rows);
        TPL::assign('category_list',$this->model('system')->fetch_category_data('goods'));

        if (is_mobile() AND !$_GET['ignore_ua_check']) {

            TPL::output('aomas/m_all_product/index');

        }else{
            TPL::output('aomas/all_product/index');

        }


    }

	public function index_action()
	{
		if ($_POST['q'])
		{
			$url = '/search/q-' . base64_encode($_POST['q']);

			if ($_GET['is_recommend'])
			{
				$url .= '__is_recommend-1';
			}

			HTTP::redirect($url);
		}

		$keyword = htmlspecialchars(base64_decode($_GET['q']));

		$this->crumb($keyword, '/search/q-' . urlencode($keyword));

		if (!$keyword)
		{
			HTTP::redirect('/');
		}

		TPL::assign('keyword', $keyword);
		TPL::assign('split_keyword', implode(' ', $this->model('system')->analysis_keyword($keyword)));

		TPL::output('search/index');
	}
}